/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml.form;

import java.util.LinkedList;
import java.util.List;

import pikes.core.Printable;
import pikes.core.PrintableCharSequence;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;select/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">TABLE
 * element</a> in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.3">The TABLE element in HTML specification</a>
 */
public class Select extends FormControl {

	private List<Option> options = new LinkedList<Option>();

	private CharSequence name = null;

	private CharSequence onFocus = null;

	private CharSequence onBlur = null;

	private Printable onChange = null;

	/**
	 * @return "select"
	 */
	@Override
	protected CharSequence getTagName() {
		return "select";
	}

	public Select(Option first) {
		Assert.notNull(first);
		options.add(first);
	}

	public void add(Option option) {
		if (option != null) {
			options.add(option);
		}
	}

	@Override
	protected void decorateCreatedTag(XMLTag tag) {
		if (name != null) {
			tag.addAttribute("name", name);
		}

		if (onFocus != null) {
			tag.addAttribute("onfocus", onFocus);
		}

		if (onBlur != null) {
			tag.addAttribute("onblur", onBlur);
		}

		if (onChange != null) {
			tag.addAttribute("onchange", onChange);
		}

		for (Option option : options) {
			option.decorateTag(tag);
		}
	}

	public CharSequence getName() {
		return name;
	}

	public void setName(CharSequence name) {
		this.name = name;
	}

	public CharSequence getOnBlur() {
		return onBlur;
	}

	public void setOnBlur(CharSequence onBlur) {
		this.onBlur = onBlur;
	}

	public Printable getOnChange() {
		return onChange;
	}

	public void setOnChange(Printable onChange) {
		this.onChange = onChange;
	}

	public void setOnChange(CharSequence onChange) {
		this.setOnChange(new PrintableCharSequence(onChange));
	}

	public CharSequence getOnFocus() {
		return onFocus;
	}

	public void setOnFocus(CharSequence onFocus) {
		this.onFocus = onFocus;
	}
}
